//
//  AMPSNativeExpressListViewController.m
//  AdScopeDemo
//
//  Created by Cookie on 2023/6/11.
//

#import "AMPSNativeExpressListViewController.h"
#import <AdScopeFoundation/AdScopeFoundation.h>
#import <AMPSAdSDK/AMPSAdSDK.h>
#import "AdScopeDemoMacro.h"
#import "AdScopeDemoAnyControlTool.h"
#import "AdScopeDemoStatusObject.h"
#import "AdScopeDemoLogger.h"
#import "AdScopeProgressHUD.h"
#import "ASNPNavtiveExpressTableViewCell.h"
#import "ASNPPlaceholderTableViewCell.h"
#import "MJRefresh.h"

static NSString * const kNativeInfoTableViewCell = @"kNativeInfoTableViewCell";
static NSString * const kNativeAdTableViewCell = @"kNativeAdTableViewCell";

@interface AMPSNativeExpressListViewController () <UITextFieldDelegate, AMPSUnifiedNativeManagerDelegate, AMPSUnifiedNativeViewDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) AMPSUnifiedNativeManager *nativeManager;

@property (nonatomic, strong) UITableView *infoTableView;

@property (nonatomic, strong) NSMutableArray *adViewArray;

@property (nonatomic, strong) AdScopeProgressHUD *loadHud;

@end

@implementation AMPSNativeExpressListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.infoTableView];
    self.infoTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    [self loadAd];
}

- (void)refreshAction {
    for (int i=0; i<_adViewArray.count; i++) {
        AMPSUnifiedNativeView *nativeView = [_adViewArray objectAtIndex:i];
        [nativeView unregisterNativeAdDataObject];
        [self.adViewArray removeObject:nativeView];
    };
    [self loadAd];
}

- (void)loadAd {
    NSLog(@"ampsNative loadAd");
    [self.nativeManager loadUnifiedNativeManager];
}

- (void)ampsNativeAdLoadSuccess:(AMPSUnifiedNativeManager *)nativeAd {
    CookieAMPSLog(@"ampsNativeAdLoadSuccess:%ld", nativeAd.adArray.count);
    CookieAMPSLog(@"成功渠道：%@-----id---%@", nativeAd.successAdInfo.adapterClassName,nativeAd.successAdInfo.adapterSpaceId);
    for (AMPSAdLoadedInfo *info in nativeAd.failAdInfoList.copy) {
        CookieAMPSLog(@"失败渠道：%@，原因：%@",info.adapterClassName, info.errorMsg);
    }
    if (nativeAd.adArray.count == 0) {
        return;
    }
    for (int i = 0; i < nativeAd.adArray.count; i++) {
        AMPSUnifiedNativeAd *mNativeAd = [nativeAd.adArray objectAtIndex:i];
        AMPSUnifiedNativeView *view = [[AMPSUnifiedNativeView alloc] init];
        view.delegate = self;
        view.viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        [view refreshData:mNativeAd];
        [self.adViewArray addObject:view];
        CookieAMPSLog(@"价格：%ld--------%@", view.eCPM,view);
    }
    [self.loadHud hideAnimated:YES];
    [self.infoTableView.mj_header endRefreshing];
    [self.infoTableView.mj_footer endRefreshing];
}

- (void)ampsNativeAdLoadFail:(AMPSUnifiedNativeManager *)nativeAd
                       error:(NSError *_Nullable)error {
    NSLog(@"---------%@",error);
}

- (void)ampsNativeAdRenderSuccess:(AMPSUnifiedNativeView *)nativeView {
    CookieAMPSLog(@"ampsNativeAdRenderSuccess:%@", nativeView);
//    [self.adViewArray addObject:nativeView];
    [self.infoTableView reloadData];
}

- (void)ampsNativeAdRenderFail:(AMPSUnifiedNativeView *)nativeView
                         error:(NSError *_Nullable)error {
    CookieAMPSLog(@"ampsNativeAdRenderFail");
    [self.adViewArray removeObject:nativeView];
}

- (void)ampsNativeAdExposured:(AMPSUnifiedNativeView *)nativeView {
    CookieAMPSLog(@"ampsNativeAdExposured");
}

- (void)ampsNativeAdDidClick:(AMPSUnifiedNativeView *)nativeView {
    CookieAMPSLog(@"ampsNativeAdDidClick");
}

- (void)ampsNativeAdDidClose:(AMPSUnifiedNativeView *)nativeView {
    CookieAMPSLog(@"ampsNativeAdDidClose--------%@",nativeView);
    [self.adViewArray removeObject:nativeView];
    [nativeView unregisterNativeAdDataObject];
    [self.infoTableView reloadData];
}

#pragma mark 懒加载
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ((indexPath.row % 5) == 0 && indexPath.row/5 < self.adViewArray.count) {
        ASNPNavtiveExpressTableViewCell *cell = [ASNPNavtiveExpressTableViewCell cellWithTableView:tableView forIndexPath:indexPath];
        [cell renderNativeAdView:[self.adViewArray objectAtIndex:indexPath.row/5]];
        return cell;
    } else {
        ASNPPlaceholderTableViewCell *cell = [ASNPPlaceholderTableViewCell cellWithTableView:tableView];
        return cell;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.adViewArray.count > 0) {
        return self.adViewArray.count*5;
    }
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 5 == 0 && indexPath.row/5 < self.adViewArray.count) {
        UIView *view = [self.adViewArray objectAtIndex:indexPath.row/5];
        return view.adScopeFrameHeight;
    } else {
        return 200;
    }
}

#pragma mark 懒加载
- (UITableView *)infoTableView {
    if (!_infoTableView) {
        _infoTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds)-kAdScopeDemoStatusBarHeight) style:UITableViewStylePlain];
        _infoTableView.backgroundColor = [UIColor whiteColor];
        _infoTableView.delegate = self;
        _infoTableView.dataSource = self;
        [_infoTableView registerClass:[ASNPPlaceholderTableViewCell class] forCellReuseIdentifier:kNativeInfoTableViewCell];
        [_infoTableView registerClass:[ASNPNavtiveExpressTableViewCell class] forCellReuseIdentifier:kNativeAdTableViewCell];
        _infoTableView.tableHeaderView = [UIView new];
        _infoTableView.tableFooterView = [UIView new];
    }
    return _infoTableView;
}

- (void)startLoadHud {
    self.loadHud = [AdScopeProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    self.loadHud.contentColor = [UIColor adScopeColorWithHexString:@"FFFFFF"];
    self.loadHud.backgroundView.color = [UIColor adScopeColorWithHexString:@"99999999"];
}

- (AdScopeProgressHUD *)loadHud {
    if (!_loadHud) {
        _loadHud = [[AdScopeProgressHUD alloc]initWithView:self.navigationController.view];
        _loadHud.contentColor = [UIColor adScopeColorWithHexString:@"FFFFFF"];
        _loadHud.backgroundView.color = [UIColor adScopeColorWithHexString:@"99999999"];
    }
    return _loadHud;
}

- (NSMutableArray *)adViewArray {
    if (!_adViewArray) {
        _adViewArray = [[NSMutableArray alloc]init];
    }
    return _adViewArray;
}

- (AMPSUnifiedNativeManager *)nativeManager {
    if (!_nativeManager) {
        AMPSAdConfiguration *cfg = [[AMPSAdConfiguration alloc]init];
        cfg.viewController = self;
        cfg.adSize = CGSizeMake(self.view.frame.size.width, 0);
        cfg.adCount = 1;
        _nativeManager = [[AMPSUnifiedNativeManager alloc]initWithSpaceId:self.spaceId adConfiguration:cfg];
        _nativeManager.delegate = self;
    }
    return _nativeManager;
}

- (void)dealloc {
    for (int i=0; i<_adViewArray.count; i++) {
        AMPSUnifiedNativeView *nativeView = [_adViewArray objectAtIndex:i];
        [nativeView unregisterNativeAdDataObject];
        [self.adViewArray removeObject:nativeView];
    };
    if (_nativeManager) {
        [_nativeManager removeUnifiedNativeManager];
        _nativeManager = nil;
    }
    CookieAMPSLog(@"ampsNativeViewController dealloc");
}

@end
